package com.yimils.core.goods.serivce.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yimils.core.goods.config.SupplierConstant;
import com.yimils.core.goods.dto.SupplierAddReq;
import com.yimils.core.goods.dto.SupplierUpdateReq;
import com.yimils.core.goods.entity.SupplierEntity;
import com.yimils.core.goods.mapper.SupplierMapper;
import com.yimils.core.goods.serivce.SupplierService;
import com.yimils.core.goods.vo.SupplierRes;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service("supplierService")
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, SupplierEntity> implements SupplierService {
    @Override
    public Boolean saveSupplier(SupplierAddReq supplierAddReq) {
        SupplierEntity supplierEntity = new SupplierEntity();
        supplierEntity.setAuditState(SupplierConstant.SupplierAuditState.WAIT.getCode());
        supplierEntity.setIsAbleState(SupplierConstant.SupplierIsAbleState.INABLE.getCode());
        supplierEntity.setSupplierAccount(supplierAddReq.getSupplierAccount());
        supplierEntity.setSupplierName(supplierAddReq.getSupplierName());
        supplierEntity.setCreateTime(LocalDateTime.now());
        supplierEntity.setUpdateTime(LocalDateTime.now());
        return save(supplierEntity);
    }

    @Override
    public Boolean updateSupplier(SupplierUpdateReq supplierUpdateReq) {
        QueryWrapper<SupplierEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(SupplierEntity::getId, supplierUpdateReq.getSupplierId())
                .eq(SupplierEntity::getState, Boolean.FALSE)
                .eq(SupplierEntity::getAuditState, SupplierConstant.SupplierAuditState.PASS.getCode())
                .eq(SupplierEntity::getIsAbleState, SupplierConstant.SupplierIsAbleState.INABLE.getCode());
        SupplierEntity entity = getOne(qw);
        entity.setSupplierAccount(supplierUpdateReq.getSupplierAccount());
        entity.setSupplierName(supplierUpdateReq.getSupplierName());
        entity.setAuditState(SupplierConstant.SupplierAuditState.WAIT.getCode());
        entity.setUpdateTime(LocalDateTime.now());
        return saveOrUpdate(entity);
    }

    @Override
    public Boolean deleteSupplier(List<Long> supplierIds) {
        QueryWrapper<SupplierEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .in(SupplierEntity::getId, supplierIds)
                .eq(SupplierEntity::getState, Boolean.FALSE)
                .eq(SupplierEntity::getAuditState, SupplierConstant.SupplierAuditState.PASS.getCode())
                .eq(SupplierEntity::getIsAbleState, SupplierConstant.SupplierIsAbleState.INABLE.getCode());
        List<SupplierEntity> entities = list(qw);
        entities.forEach(o -> {
            o.setIsAbleState(SupplierConstant.SupplierIsAbleState.DISABLE.getCode());
            o.setUpdateTime(LocalDateTime.now());
        });
        return saveOrUpdateBatch(entities);
    }

    @Override
    public List<SupplierRes> listSupplier() {
        QueryWrapper<SupplierEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(SupplierEntity::getState, Boolean.FALSE)
                .eq(SupplierEntity::getAuditState, SupplierConstant.SupplierAuditState.PASS.getCode())
                .eq(SupplierEntity::getIsAbleState, SupplierConstant.SupplierIsAbleState.INABLE.getCode());
        List<SupplierEntity> entities = list(qw);
        return entities.stream().map(o -> {
            SupplierRes supplierRes = new SupplierRes();
            supplierRes.setSupplierId(o.getId());
            supplierRes.setSupplierAccount(o.getSupplierAccount());
            supplierRes.setSupplierName(o.getSupplierName());
            return supplierRes;
        }).collect(Collectors.toList());
    }

    @Override
    public Boolean audit(Long supplierId, String auditState) {
        QueryWrapper<SupplierEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(SupplierEntity::getId, supplierId)
                .eq(SupplierEntity::getState, Boolean.FALSE)
                .eq(SupplierEntity::getIsAbleState, SupplierConstant.SupplierIsAbleState.INABLE.getCode());
        SupplierEntity entity = getOne(qw);
        String code = SupplierConstant.SupplierIsAbleState.getTypeCode(auditState);
        if (code != null) {
            entity.setAuditState(code);
            entity.setUpdateTime(LocalDateTime.now());
        }
        return saveOrUpdate(entity);
    }

    @Override
    public Boolean isAble(Long supplierId, String isAbleState) {
        QueryWrapper<SupplierEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .eq(SupplierEntity::getId, supplierId)
                .eq(SupplierEntity::getState, Boolean.FALSE)
                .eq(SupplierEntity::getIsAbleState, SupplierConstant.SupplierAuditState.PASS.getCode());
        SupplierEntity entity = getOne(qw);
        String code = SupplierConstant.SupplierIsAbleState.getTypeCode(isAbleState);
        if (code != null) {
            entity.setIsAbleState(code);
            entity.setUpdateTime(LocalDateTime.now());
        }
        return saveOrUpdate(entity);
    }
}
